package DP.P1048;/*
 *   exercise
 *   DP.P1002.P1048
 *
 *   description:
 *   @data 2025/5/13     下午3:26
 *   @author 凉风
 */

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        int m = sc.nextInt();
        int[] time = new int[m];
        int[] value = new int[m];
        for (int i = 0; i < m; i++) {
            time[i] = sc.nextInt();
            value[i] = sc.nextInt();
        }

        int[] dp = new int[t + 1];
        for (int i = 0; i < m; i++) {
            for (int j = t; j >= time[i]; j--) {
                dp[j] = Math.max(dp[j], dp[j - time[i]] + value[i]);
            }
        }
        System.out.println(dp[t]);
    }
}
